package com.web.dao.impl;

import com.web.dao.StudentDAO;
import com.web.entity.Student;
import com.web.util.JDBCUtils;

import java.util.List;

/**
 * 学生数据访问实现类
 */
public class StudentDAOImpl implements StudentDAO {
    @Override
    public List<Student> selectAllStudents() {
        return JDBCUtils.query(Student.class, "select * from student");
    }

    @Override
    public List<Student> findStudentkeyword(String keyword) {
        return JDBCUtils.query(Student.class, "select * from student where name like '%" + keyword + "%'");
    }

   @Override
   public Student findStudentById(int id) {
        List<Student> students = JDBCUtils.query(Student.class, "select * from student where id = ?", id);
        if (students.size() > 0) {
            return students.get(0);
        }
        return null;
    }
    @Override
    public boolean addStudent(Student student) {
        return JDBCUtils.update("insert into student(name,age,gender,address,cellphone) values(?,?,?,?,?)",
                student.getName(), student.getAge(), student.getGender(), student.getAddress(), student.getCellphone()) > 0;
    }
    @Override

    public boolean updateStudent(Student student) {
        String sql = "UPDATE student SET name = ?, age = ?, gender = ? WHERE id = ?";
        return JDBCUtils.update(sql, student.getName(), student.getAge(), student.getGender(), student.getId()) > 0;
    }


    @Override
    public boolean deleteStudent(int id) {
        return JDBCUtils.update("delete from student where id = ?", id) > 0;
    }
}